package cn.lzd.ui.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import cn.lzd.ui.R;

/**
 * @author lzd
 * 圆环形进度条 / 倒计时 控件
 */
public class ProgressCircleView extends View {
    private int pathLength = 70;
    private static final int perStepLength = 2;
    // region 画笔及颜色
    private Paint progressPaint;
    private int progressColorId = R.color.base_ui_warn;
    private int progressBgColorId = R.color.base_ui_transparent;
    // endregion

    // region 圆尺寸，比例，及宽度 （数值为以 viewportSize 为比例计算，按比缩放，不是绝对值）
    // 视图总大小
    private float viewportSize = 24;
    // 圆环宽度
    private float ringWidth = 1;
    // 圆半径
    private float ringRadius = 11;
    // endregion

    private RectF oval;

    public ProgressCircleView(Context context) {
        super(context);
        init();
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ProgressCircleView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        final TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.ProgressCircleView);
        progressColorId = typedArray.getResourceId(R.styleable.ProgressCircleView_uibase_progressColor, progressColorId);
        progressBgColorId = typedArray.getResourceId(R.styleable.ProgressCircleView_uibase_progressBgColor, progressBgColorId);
        viewportSize = typedArray.getFloat(R.styleable.ProgressCircleView_uibase_viewportSize, viewportSize);
        ringWidth = typedArray.getFloat(R.styleable.ProgressCircleView_uibase_ringWidth, ringWidth);
        ringRadius = typedArray.getFloat(R.styleable.ProgressCircleView_uibase_ringRadius, ringRadius);
        typedArray.recycle();

        init();
    }

    private void init() {
        // 上层进度条画笔
        progressPaint = new Paint();
        progressPaint.setStyle(Paint.Style.STROKE);
        progressPaint.setAntiAlias(true);

        oval = new RectF();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int x = getWidth();
        int y = getHeight();
        // region 计算真实要显示的数据： 圆半径、圆环宽度
        float realRadius = (ringRadius / viewportSize) * Math.min(x, y);
        float realRingWidth = (ringWidth / viewportSize) * Math.min(x, y);
        // endregion

        progressPaint.setStrokeWidth(realRingWidth);

        // 设置显示区域
        oval.set((float)x / 2 - realRadius, (float)y / 2 - realRadius,
                (float)x / 2 + realRadius, (float)y / 2 + realRadius);

        // 绘制底色
        progressPaint.setColor(ContextCompat.getColor(getContext(), progressBgColorId));
        canvas.drawOval(oval, progressPaint);

        // 绘制上层 progress
        progressPaint.setColor(ContextCompat.getColor(getContext(), progressColorId));
        //startAngle普通坐标系，-90为12点，sweepAngle if >0 -->顺时针 else <0 -->逆时针
        canvas.drawArc(oval, -90, 360.0f * pathLength / 100, false, progressPaint);
    }

    // 需要强制刷新
    public void setRatio(float ratio) {
        int newPathLength = (int) (ratio * 100);
        if (Math.abs(newPathLength - this.pathLength) > perStepLength) {
            int step = (newPathLength - this.pathLength) / perStepLength;
            this.pathLength += step * perStepLength;
            invalidate();
        }
    }
}
